package com.flushbonading.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flushbonading.mapper.RegistrationMapper;
import com.flushbonading.pojo.Registration;
import com.flushbonading.pojo.dto.EnrollDTO;
import com.flushbonading.pojo.dto.LoginDTO;
import com.flushbonading.pojo.dto.RegistrationDTO;
import com.flushbonading.service.IRegistrationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zlx&zyx
 * @since 2021-04-07
 */
@Service
public class RegistrationServiceImpl extends ServiceImpl<RegistrationMapper, Registration> implements IRegistrationService {

    @Resource
    private RegistrationMapper registrationMapper;

    /**
     * 登录时进行验证
     */
    @Override
    public Registration Login(final LoginDTO loginDTO) {

        final QueryWrapper<Registration> wrapper = new QueryWrapper<>();
        wrapper.eq("student_number", loginDTO.getStudentNumber());
        //md5加密验证
        wrapper.eq("password", SecureUtil.md5(loginDTO.getPassword()));

        final Registration registration = this.registrationMapper.selectOne(wrapper);
        System.out.println(registration);
        return registration;
    }

    //注册时进行插入
    @Override
    public boolean enroll(final EnrollDTO enrollDTO) {
        //构建对象
        final Registration registration = Registration.builder()
                .class_name(enrollDTO.getClassName())
                .experience(enrollDTO.getExperiences())
                .gender(enrollDTO.getGender())
                .id_card(enrollDTO.getIdCard())
                .name(enrollDTO.getName())
                .password(SecureUtil.md5(enrollDTO.getPassword()))
                .skill(enrollDTO.getSkill())
                .student_number(enrollDTO.getStudentNumber())
                .role_id(4)
                .build();
        System.out.println(registration);
        final int insert = this.registrationMapper.insert(registration);
        if (insert > 0)
            return true;
        return false;
    }

    /**
     * 通过学号查询姓名
     *
     * @param student_number 学生学号
     * @return 返回学生姓名
     **/
    @Override
    public String getNameByStudentNum(final String student_number) {
        return this.registrationMapper.getNameByStudentNum(student_number);
    }

    @Override
    public List<RegistrationDTO> getRegistrationMessage() {
        return this.registrationMapper.getRegistrationMessage();
    }

    @Override
    public int deleteRegistrationByStudentNum(final String student_number) {
        System.out.println("执行了删除");
        return this.registrationMapper.deleteRegistrationByStudentNum(student_number);
    }

    @Override
    public int updateAdmitByStudentNum(final String student_num, final boolean admit) {
        return this.registrationMapper.updateAdmitByStudentNum(student_num, admit);
    }

    @Override
    public int counts() {
        return this.registrationMapper.counts();
    }

    @Override
    public List<RegistrationDTO> getRegistrationMessageByNum(final String student_number) {
        return this.registrationMapper.getRegistrationMessageByNum(student_number);
    }


}
